*** dyadic data to conflict level data

import excel "ward_1_3-1 .xls", sheet("ward_1_3-1") firstrow clear

save "ward_data.dta", replace

import excel "ucdp-dyadic-221.xlsx", sheet("ucdp-dyadic-221") firstrow clear

save "ucdpdyadic_data.dta", replace

import delimited "translate_dyad.csv", varnames(1) clear 
rename new_id dyad_id
rename old_id dyadid_old

joinby dyad_id using "ucdpdyadic_data.dta", unmatched(master)
rename _merge merge1

save "ucdpdyadic_data.dta", replace


use "ward_data.dta", clear
 

 *manually expanded cases where there were multiple dyadids for the same dyad_id. These will be collapsed again when it is coverted to conflict level data. These cases are now marked 
 *Cases affected: Algeria- AQIM, Gov Serbia- Croation irregulars; Gov Portugal- FNLA; Iraq-Syria; Mali- MUJAO, Morocco-Polisario; South Sudan- SSDM/A; 
 
 replace dyadid_old="." if dyadid_old=="4, 806, 863"
 replace dyadid_old="." if dyadid_old=="273, 301"
 replace dyadid_old="." if dyadid_old=="568, 739"
 replace dyadid_old="." if dyadid_old=="809, 10961, 363"
 replace dyadid_old="." if dyadid_old=="14620, 867, 448"
 replace dyadid_old="." if dyadid_old=="862, 845"
 replace dyadid_old="." if dyadid_old=="98, 745"
 replace dyadid_old="." if dyadid_old=="818, 808"
 replace dyadid_old="." if dyadid_old=="819, 819"
 replace dyadid_old="." if dyadid_old=="7, 740"
 replace dyadid_old="." if dyadid_old=="300, 291"
destring dyadid_old, replace
 
 save "ward_expand.dta", replace
 
 import excel "ward_dyad_expand.xlsx", sheet("Sheet1") firstrow clear
 
save "WARD expanded_obs.dta", replace

use  "ward_expand.dta", clear

append using "WARD expanded_obs.dta"
drop if dyadid_old==.
 
 save "WARD expanded_obs.dta", replace

 joinby dyadid_old using "ucdpdyadic_data.dta", unmatched(master)
 
 save "dyadic_data.dta", replace

 import delimited "ACD2EPR-2021.csv", varnames(1) clear 

rename dyadid dyad_id

save "ACD2EPR.dta", replace


use "dyadic_data.dta", clear

rename _merge merge3

joinby dyad_id using "ACD2EPR.dta", unmatched(master)


save "dyadic_data.dta", replace

import delimited "Henshaw_et_al_2019.csv", varnames(1) clear 

rename dyadid dyadid_old

save "Henshaw_et_al_2019.dta", replace

use "dyadic_data.dta", clear

rename _merge merge4

joinby dyadid_old using "Henshaw_et_al_2019.dta", unmatched(master)


collapse (max)leaders_lt (max)leaders_ht (max)claim (max)recruitment (max)support, by (conflict_id year)


save "dyadic_data.dta", replace


